cc.Class({
    extends: cc.Component,

    properties: {
        spHead:cc.Sprite,
        spClothing:cc.Sprite,
        age:null,
        shengGao:null,
        tiWen:null,
        tiZhong:null,
        userCondition:null,
        passlevelCondition:null,
        isPass:null,
    },

    setUserData:function(condition, userCondition, passlevelCondition, gameScene){
        var isPass = this.contrastUserCondition(condition,userCondition);
        this.isPass = isPass;
        if(isPass)
        {
            this.tiWen = passlevelCondition;
        }
        else 
        {
            this.tiWen = window.getRandom(37.4, 40, 1);
        }

        this.shengGao = window.getRandom(165, 182, 1);
        this.tiZhong = window.getRandom(50, 65, 1);
        this.age = window.getRandom(16, 65);

        var randHeadNum;
        if(this.age > 30)randHeadNum = window.getRandom(0,1);
        else randHeadNum = window.getRandom(2,5);

        window.gameConfig.getSpriteHeadOrClothingByType("head", "head_"+randHeadNum, this.setUserHeadSprite.bind(this));
        var clothingNum = window.getRandom(0,7);
        window.gameConfig.getSpriteHeadOrClothingByType("clothing", "clothing_" + clothingNum, this.setUserClothingSprite.bind(this));
    },

    contrastUserCondition:function(condition,userCondition){
        var levelCondition = condition.split("_");
        var userCondition = userCondition.split("_");

        var conditionArr = [];
        for(let i = 0; i < levelCondition.length; i++)
        {
            //跳过不用判断的条件
            if(userCondition[i] === "N")
            {
                continue;
            }
            //判断是否满足条件 
            //可能有多种需要满足的条件  先把结果塞进数组  最后进行判断
            //条件满足为true 反之为false
            else if(levelCondition[i] === "1" && userCondition[i] === "Y")
            {
                conditionArr.push(true);
            }
            else
            {
                conditionArr.push(false)
            }
        }

        //需要满足的项目最多为两项
        //先判断数组长度
        if(conditionArr.length > 1)
        {
            //对比两项条件是否相等  
            //相等直接返回条件 2个true 或者是 2个false
            if(conditionArr[0] === conditionArr[1])
            {
                return conditionArr[0];
            }
            else
            {
                //不相等表示游戏玩家其中一项为false  不能通过
                return false;
            }
        }
        else
        {
            //直接返回结果
            return conditionArr[0];
        }
        
    },

    setUserHeadSprite:function(spriteFrame){
        this.spHead.spriteFrame = spriteFrame;
    },

    setUserClothingSprite:function(spriteFrame){
        this.spClothing.spriteFrame = spriteFrame;
    },

    setUserState:function(state){
        var headUrl;
        var number = this.spHead.spriteFrame.name;
        if(state)headUrl = number+"_win";
        else headUrl = number+"_lose";

        console.log("userState-----------------",headUrl)
        window.onLoadSpriteRes("head/" + headUrl, cc.SpriteFrame, this.setUserHeadSprite.bind(this));
    },
});
